﻿/*-------------------------------------------------------------------------
* 命名空间: NavigateJournalSimple.ViewModels/MainViewModel
* 类       名: MainViewModel
* 功      能:  TD
* 时      间:  2023/12/22 10:23:06
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using NavigateJournalSimple.Comman;
using NavigateJournalSimple.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace NavigateJournalSimple.ViewModels
{
    public class MainViewModel : BindableBase
    {
        private readonly IRegionManager regionmanager;
        private IRegionNavigationJournal navJournal;

        public MainViewModel(IRegionManager regionmanager, IRegionNavigationJournal navJournal)
        {
            this.regionmanager = regionmanager;
            this.navJournal = navJournal;

            OpenViewACommand = new DelegateCommand(DoOpenViewA);
            OpenViewBCommand = new DelegateCommand(DoOpenViewB);
            GoBackCommand = new DelegateCommand(DoGoBack);
            GoForwardCommand = new DelegateCommand(DoGoForward);
        }

        private void DoGoForward()
        {
            if (navJournal.CanGoForward)
            {
                navJournal.GoForward();
            }
        }

        private void DoGoBack()
        {
            if (navJournal.CanGoBack)
            {
                navJournal.GoBack();
            }
        }

        private void DoOpenViewB()
        {
            try
            {
                NavigationParameters parameters = new NavigationParameters();
                parameters.Add("Content", "Hello,ViewB");
                regionmanager.Regions[GlobalConstant.MainContentRegion].RequestNavigate(nameof(ViewB), navCallback, parameters);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常提示框");
            }
        }

        private void navCallback(NavigationResult navResult)
        {
            if (navResult != null && (bool)navResult.Result!)
            {
                navJournal = navResult.Context.NavigationService.Journal;
            }
        }

        private void DoOpenViewA()
        {
            try
            {
                regionmanager.Regions[GlobalConstant.MainContentRegion].RequestNavigate(nameof(ViewA), navCallback);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常提示框");
            }
        }

        public DelegateCommand GoBackCommand { get; private set; }
        public DelegateCommand GoForwardCommand { get; private set; }
        public DelegateCommand OpenViewACommand { get; private set; }
        public DelegateCommand OpenViewBCommand { get; private set; }
    }
}
